package cn.brian.modular.sys.consumer;


import cn.brian.modular.sys.service.SysUserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Component
@RocketMQMessageListener(consumerGroup = "consumer_group_txmsg_batch2",topic = "topic_txmsg_batch2")
public class TxmsgBatchConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    private ServiceImpl serviceImpl;
    //接收消息
    @Override
    public void onMessage(MessageExt message) {
        System.out.printf("开始消费消息:{%s}",message);
        //解析消息
        String messageString = new String((byte[]) message.getBody());
        JSONObject jsonObject = JSONObject.parseObject(messageString);

        String sysUserString = jsonObject.getString("sysUser");
        String classString=jsonObject.getString("dataobject");
        System.out.printf(classString);

        Object c= null;
        try {
            c = Class.forName(classString).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //sysUserStr（json）SysUser
         JSONArray platformArray=JSONArray.parseArray(sysUserString);

        List e=new ArrayList();
        for (Object jsonObject11 : platformArray) {
            Object c1 = JSONObject.parseObject(jsonObject11.toString(), c.getClass());

            e.add(c1);
        }

        //List<?>[] objects=JSONObject.parseArray(sysUserString,c.getClass());


        //转成AccountChangeEvent
//        SysUser sysUser = JSONObject.parseObject(sysUserString, SysUser.class);

        serviceImpl.saveBatch(e);


        //更新时要判断最后的时间戳是否大于当前数据库的数据的时间
    }
}
